from game.client.gui.widget import Widget
import pygame

class Container(Widget):
    def __init__(self, **args):
        Widget.__init__(self, **args)
        self.children = []


    def add_child(self, ch, zindex=0):
        ch.set_parent(self)
        i = 0
        inserted = 0
        for z, w in self.children:
            i += 1
            w.pos.validate()
            if z > zindex and not inserted:
                self.children.insert(i, (zindex, ch))
                inserted = 1

        if not inserted:
            self.children.append((zindex, ch))

        self.redraw()

    def remove_child(self, ch):
        for z, w in self.children:
            if w == ch:
                self.children.remove((z, w))
        self.redraw()

    def event(self, event, ms):
        for z, w in self.children:
            w.event(event, ms)

    def update(self, ms):
        for z, w in self.children:
            w.update(ms)

    def draw(self):
        for z, w in self.children:
            w.draw()